package net.cyue.web.easyquery.core.db.data;

import java.util.*;

/**
 * SQL查询构建器，支持Builder模式、链式设置和联表查询
 */
public class QueryWrapper {
    private String tableName;
    private List<String> columns = new ArrayList<>();
    private List<String> whereConditions = new ArrayList<>();
    private Map<String, String> conditionColumnMap = new HashMap<>(); // 跟踪条件对应的列名
    private List<String> groupByColumns = new ArrayList<>();
    private List<String> havingConditions = new ArrayList<>();
    private List<String> orderByColumns = new ArrayList<>();
    private List<JoinClause> joinClauses = new ArrayList<>();
    private Integer limit;
    private Integer offset;
    private boolean distinct;

    // 用于处理OR条件的临时列表和列跟踪
    private List<String> tempOrConditions;
    private Map<String, String> tempOrConditionColumnMap;

    /**
     * 联表查询子句
     */
    private static class JoinClause {
        enum JoinType {
            INNER, LEFT, RIGHT, FULL
        }

        JoinType type;
        String table;
        String condition;

        JoinClause(JoinType type, String table, String condition) {
            this.type = type;
            this.table = table;
            this.condition = condition;
        }

        @Override
        public String toString() {
            String joinTypeStr;
            switch (type) {
                case INNER:
                    joinTypeStr = "INNER JOIN";
                    break;
                case LEFT:
                    joinTypeStr = "LEFT JOIN";
                    break;
                case RIGHT:
                    joinTypeStr = "RIGHT JOIN";
                    break;
                case FULL:
                    joinTypeStr = "FULL JOIN";
                    break;
                default:
                    joinTypeStr = "JOIN";
            }
            return joinTypeStr + " " + table + " ON " + condition;
        }
    }

    /**
     * 构造函数
     */
    public QueryWrapper() {}

    // 私有构造函数，用于Builder模式
    private QueryWrapper(
        String tableName,
        List<String> columns,
        List<String> whereConditions,
        Map<String, String> conditionColumnMap,
        List<String> groupByColumns,
        List<String> havingConditions,
        List<String> orderByColumns,
        List<JoinClause> joinClauses,
        Integer limit,
        Integer offset,
        boolean distinct
    ) {
        this.tableName = tableName;
        this.columns = new ArrayList<>(columns);
        this.whereConditions = new ArrayList<>(whereConditions);
        this.conditionColumnMap = new HashMap<>(conditionColumnMap);
        this.groupByColumns = new ArrayList<>(groupByColumns);
        this.havingConditions = new ArrayList<>(havingConditions);
        this.orderByColumns = new ArrayList<>(orderByColumns);
        this.joinClauses = new ArrayList<>(joinClauses);
        this.limit = limit;
        this.offset = offset;
        this.distinct = distinct;
    }

    /**
     * 创建一个新的QueryWrapper实例
     */
    public static QueryWrapper create() {
        return new QueryWrapper();
    }

    /**
     * 设置表名
     */
    public QueryWrapper table(String tableName) {
        this.tableName = tableName;
        return this;
    }

    /**
     * 添加查询列
     */
    public QueryWrapper select(String... columns) {
        this.columns.addAll(Arrays.asList(columns));
        return this;
    }

    /**
     * 设置是否使用DISTINCT关键字
     */
    public QueryWrapper distinct(boolean distinct) {
        this.distinct = distinct;
        return this;
    }

    /**
     * 内连接
     * @param table 要连接的表名
     * @param condition 连接条件
     */
    public QueryWrapper innerJoin(String table, String condition) {
        joinClauses.add(new JoinClause(JoinClause.JoinType.INNER, table, condition));
        return this;
    }

    /**
     * 左连接
     * @param table 要连接的表名
     * @param condition 连接条件
     */
    public QueryWrapper leftJoin(String table, String condition) {
        joinClauses.add(new JoinClause(JoinClause.JoinType.LEFT, table, condition));
        return this;
    }

    /**
     * 右连接
     * @param table 要连接的表名
     * @param condition 连接条件
     */
    public QueryWrapper rightJoin(String table, String condition) {
        joinClauses.add(new JoinClause(JoinClause.JoinType.RIGHT, table, condition));
        return this;
    }

    /**
     * 全连接
     * @param table 要连接的表名
     * @param condition 连接条件
     */
    public QueryWrapper fullJoin(String table, String condition) {
        joinClauses.add(new JoinClause(JoinClause.JoinType.FULL, table, condition));
        return this;
    }

    /**
     * 添加等于条件 - 会覆盖同一列已有的条件
     */
    public QueryWrapper eq(String column, Object value) {
        String condition = column + " = " + formatValue(value);
        addOrReplaceCondition(column, condition);
        return this;
    }

    /**
     * 添加不等于条件 - 会覆盖同一列已有的条件
     */
    public QueryWrapper ne(String column, Object value) {
        String condition = column + " != " + formatValue(value);
        addOrReplaceCondition(column, condition);
        return this;
    }

    /**
     * 添加大于条件 - 会覆盖同一列已有的条件
     */
    public QueryWrapper gt(String column, Object value) {
        String condition = column + " > " + formatValue(value);
        addOrReplaceCondition(column, condition);
        return this;
    }

    /**
     * 添加小于条件 - 会覆盖同一列已有的条件
     */
    public QueryWrapper lt(String column, Object value) {
        String condition = column + " < " + formatValue(value);
        addOrReplaceCondition(column, condition);
        return this;
    }

    /**
     * 添加大于等于条件 - 会覆盖同一列已有的条件
     */
    public QueryWrapper ge(String column, Object value) {
        String condition = column + " >= " + formatValue(value);
        addOrReplaceCondition(column, condition);
        return this;
    }

    /**
     * 添加小于等于条件 - 会覆盖同一列已有的条件
     */
    public QueryWrapper le(String column, Object value) {
        String condition = column + " <= " + formatValue(value);
        addOrReplaceCondition(column, condition);
        return this;
    }

    /**
     * 添加模糊查询条件 - 会覆盖同一列已有的条件
     */
    public QueryWrapper like(String column, String value) {
        String condition = column + " LIKE '%" + escapeLikeValue(value) + "%'";
        addOrReplaceCondition(column, condition);
        return this;
    }

    /**
     * 添加左模糊查询条件 - 会覆盖同一列已有的条件
     */
    public QueryWrapper likeLeft(String column, String value) {
        String condition = column + " LIKE '%" + escapeLikeValue(value) + "'";
        addOrReplaceCondition(column, condition);
        return this;
    }

    /**
     * 添加右模糊查询条件 - 会覆盖同一列已有的条件
     */
    public QueryWrapper likeRight(String column, String value) {
        String condition = column + " LIKE '" + escapeLikeValue(value) + "%'";
        addOrReplaceCondition(column, condition);
        return this;
    }

    /**
     * 添加IN条件 - 会覆盖同一列已有的条件
     */
    public QueryWrapper in(String column, List<?> values) {
        StringJoiner joiner = new StringJoiner(",", "(", ")");
        for (Object value : values) {
            joiner.add(formatValue(value));
        }
        String condition = column + " IN " + joiner;
        addOrReplaceCondition(column, condition);
        return this;
    }

    /**
     * 添加NOT IN条件 - 会覆盖同一列已有的条件
     */
    public QueryWrapper notIn(String column, List<?> values) {
        StringJoiner joiner = new StringJoiner(",", "(", ")");
        for (Object value : values) {
            joiner.add(formatValue(value));
        }
        String condition = column + " NOT IN " + joiner;
        addOrReplaceCondition(column, condition);
        return this;
    }

    /**
     * 添加BETWEEN条件 - 会覆盖同一列已有的条件
     */
    public QueryWrapper between(String column, Object min, Object max) {
        String condition = column + " BETWEEN " + formatValue(min) + " AND " + formatValue(max);
        addOrReplaceCondition(column, condition);
        return this;
    }

    /**
     * 添加NOT BETWEEN条件 - 会覆盖同一列已有的条件
     */
    public QueryWrapper notBetween(String column, Object min, Object max) {
        String condition = column + " NOT BETWEEN " + formatValue(min) + " AND " + formatValue(max);
        addOrReplaceCondition(column, condition);
        return this;
    }

    /**
     * 添加IS NULL条件 - 会覆盖同一列已有的条件
     */
    public QueryWrapper isNull(String column) {
        String condition = column + " IS NULL";
        addOrReplaceCondition(column, condition);
        return this;
    }

    /**
     * 添加IS NOT NULL条件 - 会覆盖同一列已有的条件
     */
    public QueryWrapper isNotNull(String column) {
        String condition = column + " IS NOT NULL";
        addOrReplaceCondition(column, condition);
        return this;
    }

    /**
     * 开始OR条件组
     */
    public QueryWrapper or() {
        if (tempOrConditions == null) {
            tempOrConditions = new ArrayList<>();
            tempOrConditionColumnMap = new HashMap<>();
        }
        return this;
    }

    /**
     * 结束OR条件组
     */
    public QueryWrapper endOr() {
        if (tempOrConditions != null && !tempOrConditions.isEmpty()) {
            String orCondition = "(" + String.join(" OR ", tempOrConditions) + ")";
            whereConditions.add(orCondition);

            // 将OR条件组作为一个整体添加到主映射中，使用特殊键避免冲突
            String orGroupKey = "OR_GROUP_" + System.currentTimeMillis();
            conditionColumnMap.put(orGroupKey, orCondition);

            tempOrConditions = null;
            tempOrConditionColumnMap = null;
        }
        return this;
    }

    /**
     * 添加排序条件
     */
    public QueryWrapper orderBy(String column, boolean ascending) {
        // 检查是否已存在该列的排序条件，如果有则替换
        String newOrder = column + " " + (ascending ? "ASC" : "DESC");
        for (int i = 0; i < orderByColumns.size(); i++) {
            String existing = orderByColumns.get(i);
            if (existing.startsWith(column + " ")) {
                orderByColumns.set(i, newOrder);
                return this;
            }
        }
        orderByColumns.add(newOrder);
        return this;
    }

    /**
     * 添加GROUP BY条件
     */
    public QueryWrapper groupBy(String... columns) {
        for (String column : columns) {
            // 如果已存在则不重复添加
            if (!groupByColumns.contains(column)) {
                groupByColumns.add(column);
            }
        }
        return this;
    }

    /**
     * 添加HAVING条件
     */
    public QueryWrapper having(String condition) {
        havingConditions.add(condition);
        return this;
    }

    /**
     * 添加HAVING等于条件
     */
    public QueryWrapper havingEq(String column, Object value) {
        havingConditions.add(column + " = " + formatValue(value));
        return this;
    }

    /**
     * 设置分页限制 - 会覆盖已有的limit设置
     */
    public QueryWrapper limit(int limit) {
        this.limit = limit;
        return this;
    }

    /**
     * 设置分页偏移量 - 会覆盖已有的offset设置
     */
    public QueryWrapper offset(int offset) {
        this.offset = offset;
        return this;
    }

    /**
     * 构建SQL语句
     */
    public String build() {
        // 确保所有OR条件组都已关闭
        endOr();

        if (tableName == null || tableName.isEmpty()) {
            throw new IllegalStateException("表名不能为空");
        }

        StringBuilder sql = new StringBuilder("SELECT ");

        // 处理DISTINCT
        if (distinct) {
            sql.append("DISTINCT ");
        }

        // 处理查询列
        if (columns.isEmpty()) {
            sql.append("*");
        } else {
            sql.append(String.join(", ", columns));
        }

        // 处理表名
        sql.append(" FROM ").append(tableName);

        // 处理联表查询
        for (JoinClause joinClause : joinClauses) {
            sql.append(" ").append(joinClause);
        }

        // 处理WHERE条件
        if (!whereConditions.isEmpty()) {
            sql.append(" WHERE ").append(String.join(" AND ", whereConditions));
        }

        // 处理GROUP BY
        if (!groupByColumns.isEmpty()) {
            sql.append(" GROUP BY ").append(String.join(", ", groupByColumns));

            // 处理HAVING条件
            if (!havingConditions.isEmpty()) {
                sql.append(" HAVING ").append(String.join(" AND ", havingConditions));
            }
        }

        // 处理排序
        if (!orderByColumns.isEmpty()) {
            sql.append(" ORDER BY ").append(String.join(", ", orderByColumns));
        }

        // 处理分页
        if (limit != null) {
            sql.append(" LIMIT ").append(limit);
        }
        if (offset != null) {
            sql.append(" OFFSET ").append(offset);
        }

        return sql.toString();
    }

    public String toSQL() {
        return this.build();
    }

    /**
     * 格式化值，字符串添加单引号，#{...}格式的参数占位符不处理
     */
    private static String formatValue(Object value) {
        if (value == null) {
            return "NULL";
        }

        // 检查是否是 ? 或 ${...} 或 #{...} 格式的参数占位符，如果是则不处理
        String strValue = value.toString();
        String trimStrValue = strValue.trim();
        if (
            trimStrValue.equals("?") ||
            (trimStrValue.startsWith("${") && trimStrValue.endsWith("}")) ||
            (trimStrValue.startsWith("#{") && trimStrValue.endsWith("}"))
        ) {
            return strValue;
        }

        if (value instanceof String) {
            return "'" + ((String) value).replace("'", "''") + "'";
        }
        if (value instanceof Number || value instanceof Boolean) {
            return strValue;
        }
        // 对于其他类型，转换为字符串并添加单引号
        return "'" + strValue.replace("'", "''") + "'";
    }

    /**
     * 转义模糊查询中的特殊字符
     */
    private static String escapeLikeValue(String value) {
        if (value == null) {
            return "";
        }

        // 检查是否是 ? 或 ${...} 或 #{...} 格式的参数占位符，如果是则不处理
        String trimValue = value.trim();
        if (
            trimValue.equals("?") ||
            (trimValue.startsWith("${") && trimValue.endsWith("}")) ||
            (trimValue.startsWith("#{") && trimValue.endsWith("}"))
        ) {
            return value;
        }

        // 转义%和_
        return value.replace("\\", "\\\\")
            .replace("%", "\\%")
            .replace("_", "\\_")
            .replace("'", "''");
    }

    /**
     * 添加条件，如果同一列已有条件则替换
     */
    private void addOrReplaceCondition(String column, String condition) {
        if (tempOrConditions != null) {
            // 添加新条件，在 or 时 允许重复添加，用于构建复杂的 or 条件
            tempOrConditions.add(condition);
            tempOrConditionColumnMap.put(column, condition);
        } else {
            // 处理普通条件
            if (conditionColumnMap.containsKey(column)) {
                // 替换已存在的条件
                String existingCondition = conditionColumnMap.get(column);
                int index = whereConditions.indexOf(existingCondition);
                if (index != -1) {
                    whereConditions.set(index, condition);
                }
            } else {
                // 添加新条件
                whereConditions.add(condition);
            }
            conditionColumnMap.put(column, condition);
        }
    }

    /**
     * Builder模式
     */
    public static class Builder {
        private String tableName;
        private final List<String> columns = new ArrayList<>();
        private final List<String> whereConditions = new ArrayList<>();
        private final Map<String, String> conditionColumnMap = new HashMap<>();
        private final List<String> groupByColumns = new ArrayList<>();
        private final List<String> havingConditions = new ArrayList<>();
        private final List<String> orderByColumns = new ArrayList<>();
        private final List<JoinClause> joinClauses = new ArrayList<>();
        private Integer limit;
        private Integer offset;
        private boolean distinct;
        private List<String> tempOrConditions;
        private Map<String, String> tempOrConditionColumnMap;

        public Builder table(String tableName) {
            this.tableName = tableName;
            return this;
        }

        public Builder select(String... columns) {
            this.columns.addAll(Arrays.asList(columns));
            return this;
        }

        public Builder distinct(boolean distinct) {
            this.distinct = distinct;
            return this;
        }

        /**
         * 内连接
         */
        public Builder innerJoin(String table, String condition) {
            joinClauses.add(new JoinClause(JoinClause.JoinType.INNER, table, condition));
            return this;
        }

        /**
         * 左连接
         */
        public Builder leftJoin(String table, String condition) {
            joinClauses.add(new JoinClause(JoinClause.JoinType.LEFT, table, condition));
            return this;
        }

        /**
         * 右连接
         */
        public Builder rightJoin(String table, String condition) {
            joinClauses.add(new JoinClause(JoinClause.JoinType.RIGHT, table, condition));
            return this;
        }

        /**
         * 全连接
         */
        public Builder fullJoin(String table, String condition) {
            joinClauses.add(new JoinClause(JoinClause.JoinType.FULL, table, condition));
            return this;
        }

        public Builder eq(String column, Object value) {
            String condition = column + " = " + formatValue(value);
            addOrReplaceCondition(column, condition);
            return this;
        }

        public Builder ne(String column, Object value) {
            String condition = column + " != " + formatValue(value);
            addOrReplaceCondition(column, condition);
            return this;
        }

        public Builder gt(String column, Object value) {
            String condition = column + " > " + formatValue(value);
            addOrReplaceCondition(column, condition);
            return this;
        }

        public Builder lt(String column, Object value) {
            String condition = column + " < " + formatValue(value);
            addOrReplaceCondition(column, condition);
            return this;
        }

        public Builder ge(String column, Object value) {
            String condition = column + " >= " + formatValue(value);
            addOrReplaceCondition(column, condition);
            return this;
        }

        public Builder le(String column, Object value) {
            String condition = column + " <= " + formatValue(value);
            addOrReplaceCondition(column, condition);
            return this;
        }

        public Builder like(String column, String value) {
            String condition = column + " LIKE '%" + escapeLikeValue(value) + "%'";
            addOrReplaceCondition(column, condition);
            return this;
        }

        public Builder likeLeft(String column, String value) {
            String condition = column + " LIKE '%" + escapeLikeValue(value) + "'";
            addOrReplaceCondition(column, condition);
            return this;
        }

        public Builder likeRight(String column, String value) {
            String condition = column + " LIKE '" + escapeLikeValue(value) + "%'";
            addOrReplaceCondition(column, condition);
            return this;
        }

        public Builder in(String column, List<?> values) {
            StringJoiner joiner = new StringJoiner(",", "(", ")");
            for (Object value : values) {
                joiner.add(formatValue(value));
            }
            String condition = column + " IN " + joiner;
            addOrReplaceCondition(column, condition);
            return this;
        }

        public Builder notIn(String column, List<?> values) {
            StringJoiner joiner = new StringJoiner(",", "(", ")");
            for (Object value : values) {
                joiner.add(formatValue(value));
            }
            String condition = column + " NOT IN " + joiner;
            addOrReplaceCondition(column, condition);
            return this;
        }

        public Builder between(String column, Object min, Object max) {
            String condition = column + " BETWEEN " + formatValue(min) + " AND " + formatValue(max);
            addOrReplaceCondition(column, condition);
            return this;
        }

        public Builder notBetween(String column, Object min, Object max) {
            String condition = column + " NOT BETWEEN " + formatValue(min) + " AND " + formatValue(max);
            addOrReplaceCondition(column, condition);
            return this;
        }

        public Builder isNull(String column) {
            String condition = column + " IS NULL";
            addOrReplaceCondition(column, condition);
            return this;
        }

        public Builder isNotNull(String column) {
            String condition = column + " IS NOT NULL";
            addOrReplaceCondition(column, condition);
            return this;
        }

        public Builder or() {
            if (tempOrConditions == null) {
                tempOrConditions = new ArrayList<>();
                tempOrConditionColumnMap = new HashMap<>();
            }
            return this;
        }

        public Builder endOr() {
            if (tempOrConditions != null && !tempOrConditions.isEmpty()) {
                String orCondition = "(" + String.join(" OR ", tempOrConditions) + ")";
                whereConditions.add(orCondition);

                // 将OR条件组作为一个整体添加到主映射中，使用特殊键避免冲突
                String orGroupKey = "OR_GROUP_" + System.currentTimeMillis();
                conditionColumnMap.put(orGroupKey, orCondition);

                tempOrConditions = null;
                tempOrConditionColumnMap = null;
            }
            return this;
        }

        public Builder orderBy(String column, boolean ascending) {
            // 检查是否已存在该列的排序条件，如果有则替换
            String newOrder = column + " " + (ascending ? "ASC" : "DESC");
            for (int i = 0; i < orderByColumns.size(); i++) {
                String existing = orderByColumns.get(i);
                if (existing.startsWith(column + " ")) {
                    orderByColumns.set(i, newOrder);
                    return this;
                }
            }
            orderByColumns.add(newOrder);
            return this;
        }

        public Builder groupBy(String... columns) {
            for (String column : columns) {
                // 如果已存在则不重复添加
                if (!groupByColumns.contains(column)) {
                    groupByColumns.add(column);
                }
            }
            return this;
        }

        public Builder having(String condition) {
            havingConditions.add(condition);
            return this;
        }

        public Builder havingEq(String column, Object value) {
            havingConditions.add(column + " = " + formatValue(value));
            return this;
        }

        public Builder limit(int limit) {
            this.limit = limit;
            return this;
        }

        public Builder offset(int offset) {
            this.offset = offset;
            return this;
        }

        public QueryWrapper build() {
            // 确保所有OR条件组都已关闭
            endOr();
            return new QueryWrapper(
                    tableName,
                    columns,
                    whereConditions,
                    conditionColumnMap,
                    groupByColumns,
                    havingConditions,
                    orderByColumns,
                    joinClauses,
                    limit,
                    offset,
                    distinct
            );
        }

        private void addOrReplaceCondition(String column, String condition) {
            if (tempOrConditions != null) {
                // 处理OR条件组中的条件
                if (tempOrConditionColumnMap.containsKey(column)) {
                    // 替换已存在的条件
                    String existingCondition = tempOrConditionColumnMap.get(column);
                    int index = tempOrConditions.indexOf(existingCondition);
                    if (index != -1) {
                        tempOrConditions.set(index, condition);
                    }
                } else {
                    // 添加新条件
                    tempOrConditions.add(condition);
                }
                tempOrConditionColumnMap.put(column, condition);
            } else {
                // 处理普通条件
                if (conditionColumnMap.containsKey(column)) {
                    // 替换已存在的条件
                    String existingCondition = conditionColumnMap.get(column);
                    int index = whereConditions.indexOf(existingCondition);
                    if (index != -1) {
                        whereConditions.set(index, condition);
                    }
                } else {
                    // 添加新条件
                    whereConditions.add(condition);
                }
                conditionColumnMap.put(column, condition);
            }
        }
    }


    public static Builder builder() {
        return new Builder();
    }

    // 使用示例
    public static void main(String[] args) {
        // 1. 使用Builder模式构建复杂查询（包含联表）
        QueryWrapper query1 = QueryWrapper.builder()
            .table("orders")
            .select("orders.id", "users.name", "orders.amount", "orders.create_time")
            .innerJoin("users", "orders.user_id = users.id")
            .eq("orders.status", "completed")
            .between("orders.create_time", "2023-01-01", "2023-12-31")
            .or()
            .eq("users.vip_level", "gold")
            .gt("orders.amount", 1000)
            .endOr()
            .orderBy("orders.create_time", false)
            .limit(10)
            .build();
        System.out.println("1. 带联表的Builder模式SQL: " + query1.build());

        // 2. 使用链式设置构建多表连接查询
        QueryWrapper query2 = QueryWrapper.create()
            .table("products")
            .select("products.name", "categories.name as category", "brands.name as brand")
            .leftJoin("categories", "products.category_id = categories.id")
            .leftJoin("brands", "products.brand_id = brands.id")
            .notIn("products.status", Arrays.asList("discontinued", "out_of_stock"))
            .likeRight("products.name", "pro")
            .orderBy("categories.name", true)
            .orderBy("brands.name", true);
        System.out.println("2. 多表连接的链式SQL: " + query2.build());

        // 3. 包含OR条件的查询
        QueryWrapper query3 = QueryWrapper.create()
            .table("users")
            .select("users.id", "users.name", "users.email", "roles.name as role")
            .innerJoin("user_roles", "users.id = user_roles.user_id")
            .innerJoin("roles", "user_roles.role_id = roles.id")
            .eq("users.active", true)
            .or()
            .like("users.name", "admin")
            .like("roles.name", "administrator")
            .endOr()
            .orderBy("users.created_at", false);
        System.out.println("3. 带联表和OR条件的SQL: " + query3.build());

        // 4. 使用参数占位符的示例
        QueryWrapper query4 = QueryWrapper.create()
            .table("users")
            .select("id", "name", "email")
            .eq("status", "#{status}")
            .gt("create_time", "#{startTime}")
            .like("name", "${nameLike}");
        System.out.println("4. 带参数占位符的SQL: " + query4.build());

        // 测试同一列条件覆盖
        QueryWrapper query5 = QueryWrapper.create()
            .table("users")
            .select("id", "name")
            .eq("status", "active")    // 这行会被下一行覆盖
            .eq("status", "inactive")  // 这行会保留
            .like("name", "john")      // 这行会被下一行覆盖
            .like("name", "doe");      // 这行会保留

        System.out.println("5. 条件覆盖测试: " + query5.build());
        // 预期结果应包含: WHERE status = 'inactive' AND name LIKE '%doe%'
    }
}
